package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ConfigurationItemType;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DesignatedPersonnel;
import com.ruoyi.system.mapper.DesignatedPersonnelMapper;
import com.ruoyi.system.service.IDesignatedPersonnelService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 指定人员处理业务Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-24
 */
@Service
public class DesignatedPersonnelServiceImpl implements IDesignatedPersonnelService 
{
    @Autowired
    private DesignatedPersonnelMapper designatedPersonnelMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询指定人员处理业务
     * 
     * @param id 指定人员处理业务主键
     * @return 指定人员处理业务
     */
    @Override
    public DesignatedPersonnel selectDesignatedPersonnelById(Long id)
    {
        return designatedPersonnelMapper.selectDesignatedPersonnelById(id);
    }

    /**
     * 查询指定人员处理业务列表
     * 
     * @param designatedPersonnel 指定人员处理业务
     * @return 指定人员处理业务
     */
    @Override
    public List<DesignatedPersonnel> selectDesignatedPersonnelList(DesignatedPersonnel designatedPersonnel)
    {
       return designatedPersonnelMapper.selectDesignatedPersonnelList(designatedPersonnel);
    }

    /**
     * 新增指定人员处理业务
     * 
     * @param designatedPersonnel 指定人员处理业务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDesignatedPersonnel(DesignatedPersonnel designatedPersonnel)
    {
        //判断姓名工号是否存在
        SysUser user = sysUserService.selectUserByNickNameAndJobName(designatedPersonnel.getName(),designatedPersonnel.getJobNumber());
        if(user != null){
            //为该员工添加角色
            String roleName = selectRoleName(designatedPersonnel.getConfigItemName());
            SysRole sysRole = sysRoleService.selectRoleByName(roleName);
            if(!StringUtils.isNull(sysRole)){
                sysRoleService.insertAuthUsers(sysRole.getRoleId(),new Long[]{user.getUserId()});
                designatedPersonnel.setUserId(user.getUserId());
                return designatedPersonnelMapper.insertDesignatedPersonnel(designatedPersonnel);
            }else{
                throw new GlobalException(roleName+"该角色不存在");
            }
        }
        throw new GlobalException("姓名或工号不存在");
    }

    /**
     * 修改指定人员处理业务
     * 
     * @param designatedPersonnel 指定人员处理业务
     * @return 结果
     */
    @Override
    public int updateDesignatedPersonnel(DesignatedPersonnel designatedPersonnel)
    {
        //判断姓名工号是否存在
        SysUser user = sysUserService.selectUserByNickNameAndJobName(designatedPersonnel.getName(),designatedPersonnel.getJobNumber());
        if(user != null){
            if(!user.getUserId().equals(designatedPersonnel.getUserId())){
                String roleName = selectRoleName(designatedPersonnel.getConfigItemName());
                SysRole sysRole = sysRoleService.selectRoleByName(roleName);
                //删除原先用户的角色
                sysRoleService.deleteAuthUsers(sysRole.getRoleId(),new Long[]{designatedPersonnel.getUserId()});
                sysRoleService.insertAuthUsers(sysRole.getRoleId(),new Long[]{user.getUserId()});
                designatedPersonnel.setUserId(user.getUserId());
            }
            int update = designatedPersonnelMapper.updateDesignatedPersonnel(designatedPersonnel);
            return update;
        }
        throw new GlobalException("姓名或工号不存在");
    }

    /**
     * 批量删除指定人员处理业务
     * 
     * @param ids 需要删除的指定人员处理业务主键
     * @return 结果
     */
    @Override
    public int deleteDesignatedPersonnelByIds(Long[] ids)
    {
        for (Long id : ids) {
            //删除角色信息
            DesignatedPersonnel designatedPersonnel = designatedPersonnelMapper.selectDesignatedPersonnelById(id);
            String roleName = selectRoleName(designatedPersonnel.getConfigItemName());
            SysRole sysRole = sysRoleService.selectRoleByName(roleName);
            sysRoleService.deleteAuthUsers(sysRole.getRoleId(),new Long[]{designatedPersonnel.getUserId()});
        }
        return designatedPersonnelMapper.deleteDesignatedPersonnelByIds(ids);
    }

    /**
     * 删除指定人员处理业务信息
     * 
     * @param id 指定人员处理业务主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteDesignatedPersonnelById(Long id)
    {
        //删除角色信息
        DesignatedPersonnel designatedPersonnel = designatedPersonnelMapper.selectDesignatedPersonnelById(id);
        String roleName = selectRoleName(designatedPersonnel.getConfigItemName());
        SysRole sysRole = sysRoleService.selectRoleByName(roleName);
        sysRoleService.deleteAuthUsers(sysRole.getRoleId(),new Long[]{designatedPersonnel.getUserId()});
        return designatedPersonnelMapper.deleteDesignatedPersonnelById(id);
    }

    public String selectRoleName(String code){
        if(ConfigurationItemType.OVERTIME_VERIFICATION_STAFF.getCode().equals(code)){
            return ConfigurationItemType.OVERTIME_VERIFICATION_STAFF.getName();
        }else if(ConfigurationItemType.PERSONNEL_DEPARTMENT_EXAMINER.getCode().equals(code)){
            return ConfigurationItemType.PERSONNEL_DEPARTMENT_EXAMINER.getName();
        }else{
            return ConfigurationItemType.TRAINING_RECORD_VERIFIERS.getName();
        }
    }
}
